#include "CollisionComponent.h"
#include "VisualComponent.h"
#include "GameObject.h"

CollisionComponent::CollisionComponent(void) : m_callback(nullptr), m_visualComponent(nullptr), m_camera(nullptr){}
CollisionComponent::CollisionComponent(std::function<void()> p_func, vn::Camera* p_camera) : m_callback(p_func), m_visualComponent(nullptr), m_camera(p_camera){}
CollisionComponent::~CollisionComponent(void){}

void CollisionComponent::init()
{
	m_visualComponent = static_cast<VisualComponent*>(getOwner()->getComponent("VisualComponent"));
}

void CollisionComponent::update(const GameTime& p_gameTime)
{
	if (m_camera != nullptr)
	{
		vn::FloatRect rect = m_camera->getBoundary();
		if (!rect.intersect(vn::FloatRect(m_visualComponent->getSprite()->getPosition().x, m_visualComponent->getSprite()->getPosition().y, (float)m_visualComponent->getSprite()->getTextureRect().w, (float)m_visualComponent->getSprite()->getTextureRect().h)))
		{
			m_callback();
		}
	}
}
